package com.woniuxy.education.service.impl;

import com.woniuxy.education.entity.*;
import com.woniuxy.education.mapper.MarkTestsMapper;
import com.woniuxy.education.mapper.StudentMapper;
import com.woniuxy.education.service.MarkTestsService;
import com.woniuxy.education.utils.StringReorder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MarkTestsServiceImpl implements MarkTestsService {
    @Autowired
    MarkTestsMapper markTestsMapper;
    @Autowired
    StudentMapper studentMapper;

    //获取班级ID
    @Override
    public List<TeacherClass> selectClassId(String userid) {
        //查询教师Id
        String tid = markTestsMapper.selectteacherid(userid);
        if (tid != null) {
            //通过教师ID查询班级id和科目
            List<TeacherClass> cids = markTestsMapper.selectClassIdBytid(tid);
            return cids;
        }
        return null;
    }

    //查询未批改学生作业信息
    @Override
    public List selectCompletedWork(List<TeacherClass> tcs) {
        //Map map=new HashMap();
        List list = new ArrayList();
        //遍历班级
        for (int i = 0; i < tcs.size(); i++) {
            //通过班级Id和科目查询班级作业信息
            List<HomeworkAssignments> lhs = markTestsMapper.selectWorks(tcs.get(i));
            for (int j = 0; j < lhs.size(); j++) {
                //得到作业题目
                HomeworkTitle homeworkTitles = markTestsMapper.selecthomework(lhs.get(j).getId());
                //通过作业题目id查询学生答案和学生
                List<Completed_work> completedworks = markTestsMapper.selectStudents(homeworkTitles.getId());

                for (Completed_work completed_work : completedworks) {
                    //封装数据，用于前端展示
                    MarkTests markTests = new MarkTests();
                    markTests.setState("未批改");
                    //得到学生
                    Student student = completed_work.getStudent();
                    //System.out.println(student);
                    markTests.setDate(lhs.get(j).getRelease_time());
                    markTests.setSubject(lhs.get(j).getSubject());
                    markTests.setType(lhs.get(j).getJob_type());
                    markTests.setName(student.getName());
                    markTests.setNumber("" + student.getNumber());
                    markTests.setId(completed_work.getId());

                    list.add(markTests);
                }
                //   list.addAll(students);
                //map.put(lhs.get(j).getId(),students);
            }
        }


        return list;
    }

    //查询指定学生作业答案，通过学生完成作业答案id
    @Override
    public List<Allquestions> getCompletedWorkAndTitle(int hid) {
        // 查询学生答案和题目
        Completed_work completed_works =
                markTestsMapper.selectStudentCompletedWorkAndTitle(hid);


        //创建集合存储学生答案和题目
        List<Allquestions> list = new ArrayList();
        //获取作业所有题目id
        HomeworkTitle homeworkTitle = completed_works.getHomeworkTitle();

        //获取选择题题目ID
        String[] chooses = homeworkTitle.getChoose().split(",");
        //获取学生选择题所有答案
        String[] strchooses = completed_works.getChoose_answer().split("\\|&&\\|");
        //遍历题目与答案匹配
        String chooseiserror = "";
       // int u_id = completed_works.getU_id();
        //暂时写死
        int u_id =1;
        for (int i = 0; i < strchooses.length; i++) {
            //切割题号与题目
            String[] ss = strchooses[i].split(":");
            for (int j = 0; j < chooses.length; j++) {
                    //查正确答案
                    String answer = markTestsMapper.findchooseAnswer(chooses[j]).getAnswer();
                    if (answer.equals(ss[1])) {
                        continue;
                } else {
                    chooseiserror += ss[0] + ",";
                }


            }
        }
        Topics_table topics_table = studentMapper.SelectTopics_table(u_id);
        if (chooseiserror != "") {
            //去除字符串最后的一个 “，”
            String str = chooseiserror.toString().substring(0, chooseiserror.length() - 1);
            String choose_isErrorString = StringReorder.StringSort(str, topics_table.getChoose_iserror());
            studentMapper.UpdateChooseIsError(choose_isErrorString, u_id);
        }

        //获取填空题题目ID
        String[] completions = homeworkTitle.getCompletion().split(",");

        //获取学生填空题所有答案
        String[] strs = completed_works.getCompletion_answer().split("\\|&&\\|");
        //遍历答案
        for (int i = 0; i < strs.length; i++) {
            //切割题号与题目
            String[] ss = strs[i].split(":");
            //遍历题目与答案匹配
            for (int j = 0; j < completions.length; j++) {
                if (completions[j].equals(ss[0])) {
                    //封装数据
                    Allquestions allquestions = new Allquestions();
                    allquestions.setQuestion(
                            studentMapper.findCompletion(Integer.parseInt(ss[0])).getTitle());
                    allquestions.setTitleid(Integer.parseInt(ss[0]));
                    allquestions.setTitleType(2);
                    if (ss.length == 1) {
                        allquestions.setAnswer("");
                    } else
                        allquestions.setAnswer(ss[1]);
                    //存到集合
                    list.add(allquestions);
                }
            }
        }


        //获取解答题题目ID
        String[] answer_questions = homeworkTitle.getAnswer_questions().split(",");
        //获取学生解答题所有答案
        String[] stranswer_questions = completed_works.getAnswer_questions_answer().split("\\|&&\\|");
        //遍历答案
        for (int i = 0; i < stranswer_questions.length; i++) {
            String[] ss = stranswer_questions[i].split(":");
            //遍历题目与答案匹配
            for (int j = 0; j < answer_questions.length; j++) {
                if (answer_questions[j].equals(ss[0])) {
                    //封装数据
                    Allquestions allquestions = new Allquestions();
                    allquestions.setQuestion(
                            studentMapper.findAnswer_Questions(Integer.parseInt(ss[0])).getTitle());
                    allquestions.setTitleid(Integer.parseInt(ss[0]));
                    allquestions.setTitleType(3);
                    if (ss.length == 1) {
                        allquestions.setAnswer("");
                    } else
                        allquestions.setAnswer(ss[1]);
                    //存到集合
                    list.add(allquestions);
                }
            }
        }


        //获取作文题目ID
        String[] compositions = homeworkTitle.getComposition().split(",");
        //获取学生作文题所有答案
        String[] strcompositions = completed_works.getComposition_answer().split("\\|&&\\|");
        //遍历答案
        for (int i = 0; i < strcompositions.length; i++) {
            String[] ss = strcompositions[i].split(":");
            //遍历题目与答案匹配
            for (int j = 0; j < compositions.length; j++) {
                if (compositions[j].equals(ss[0])) {
                    //封装数据
                    Allquestions allquestions = new Allquestions();

                    allquestions.setQuestion(
                            studentMapper.findComposition(Integer.parseInt(ss[0])).getTitle());
                    allquestions.setTitleid(Integer.parseInt(ss[0]));
                    allquestions.setTitleType(4);
                    if (ss.length == 1) {
                        allquestions.setAnswer("");
                    } else
                        allquestions.setAnswer(ss[1]);
                    //存到集合
                    list.add(allquestions);
                }
            }
        }


        return list;
    }

    //批改作业
    @Override
    public boolean correct(List<CorrectTitle> correctTitles) {
        String strchoose = "";
        String strcompletion = "";
        String strquestionAnswer = "";
        String strcomposition = "";

//        System.out.println(titleid);
//        Integer uid=markTestsMapper.finduid(titleid);
        for (int i = 0; i < correctTitles.size(); i++) {
            //
            if ("错".equals(correctTitles.get(i).getIserror()) && correctTitles.get(i).getTitleType() == 2) {

                strcompletion += correctTitles.get(i).getTitleid() + ",";


            }

            if ("错".equals(correctTitles.get(i).getIserror()) && correctTitles.get(i).getTitleType() == 3) {

                strquestionAnswer += correctTitles.get(i).getTitleid() + ",";

            }
            if ("错".equals(correctTitles.get(i).getIserror()) && correctTitles.get(i).getTitleType() == 4) {


                strcomposition += correctTitles.get(i).getTitleid() + ",";


            }
        }
        int u_id=correctTitles.get(0).getU_id();
        Topics_table topics_table = studentMapper.SelectTopics_table(u_id);
        if(strcompletion!=""){
            //去除字符串最后的一个 “，”
            String str = strcompletion.toString().substring(0, strcompletion.length()-1);
            String completion_isErrorString=StringReorder.StringSort(str,topics_table.getCompletion_iserror());
            studentMapper.UpdateCompletionIsError(completion_isErrorString,u_id);
        }
        if(strquestionAnswer!=""){
            //去除字符串最后的一个 “，”
            String str = strquestionAnswer.toString().substring(0, strquestionAnswer.length()-1);
            studentMapper.UpdateAnswerQuestionsIsError(StringReorder.StringSort(str,topics_table.getAnswer_questions_iserror()),u_id);
        }
        if(strcomposition!=""){
            //去除字符串最后的一个 “，”
            String str = strcomposition.toString().substring(0, strcomposition.length()-1);
            studentMapper.UpdateCompositionIsError(StringReorder.StringSort(str,topics_table.getComposition_iserror()),u_id);
        }
        return true;
    }

    //试卷批改

    //查询未批改试卷信息
    @Override
    public List selectCompletedTest(List<TeacherClass> tcs) {
        //Map map=new HashMap();
        List list = new ArrayList();
        //遍历班级
        for (int i = 0; i < tcs.size(); i++) {
            //通过班级Id和科目查询班级考试信息
            List<TestMessage> tms = markTestsMapper.selectTestTable(tcs.get(i));
            for (int j = 0; j < tms.size(); j++) {
                //得到考试题目
                Titleids titleids = markTestsMapper.selectTesttitle(tms.get(j).getId() + "");

                //通过考试题目id查询学生考试答案和学生
                List<Completed_test> completedtests = markTestsMapper.selectStudentTests(titleids.getId());


                for (Completed_test completedtest : completedtests) {
                    //封装数据，用于前端展示
                    MarkTests markTests = new MarkTests();
                    markTests.setState("未批改");
                    //得到学生
                    Student student = completedtest.getStudent();
                    //发布时间
                    markTests.setDate(tms.get(j).getStart_time());
                    //科目
                    markTests.setSubject(tms.get(j).getSubject());
                    //考试类型
                    markTests.setType(tms.get(j).getType());
                    //姓名
                    markTests.setName(student.getName());
                    //学号
                    markTests.setNumber("" + student.getNumber());
                    markTests.setId(completedtest.getId());//考试答案id

                    list.add(markTests);
                }
                //   list.addAll(students);
                //map.put(lhs.get(j).getId(),students);
            }
        }


        return list;
    }

    //试卷批改
//查询指定学生试卷答案，通过学生完成试卷答案id
    @Override
    public List<Allquestions> getCompletedTestTitleAndAnswer(int tid) {
        // 查询学生答案和题目
        Completed_test completedtests =
                markTestsMapper.selectStudentCompletedTestAndTitle(tid);

        //创建集合存储学生答案和题目
        List<Allquestions> list = new ArrayList();
        //获取试卷所有题目id
        Titleids titleids = completedtests.getTitleids();

        System.out.println(titleids);
        //获取填空题题目ID
        String[] completions = titleids.getCompletion().split(",");
        //获取学生填空题所有答案
        String[] strs = completedtests.getCompletion_answer().split("\\|&&\\|");
        //遍历答案
        for (int i = 0; i < strs.length; i++) {
            //切割题号与题目
            String[] ss = strs[i].split(":");
            //遍历题目与答案匹配
            for (int j = 0; j < completions.length; j++) {
                if (completions[j].equals(ss[0])) {
                    //封装数据
                    Allquestions allquestions = new Allquestions();
                    allquestions.setQuestion(
                            studentMapper.findCompletion(Integer.parseInt(ss[0])).getTitle());
                    allquestions.setTitleid(Integer.parseInt(ss[0]));
                    allquestions.setTitleType(2);
                    if (ss.length == 1) {
                        allquestions.setAnswer("");
                    } else
                        allquestions.setAnswer(ss[1]);
                    //存到集合
                    list.add(allquestions);
                }
            }
        }


        //获取解答题题目ID
        String[] answer_questions = titleids.getAnswer_questions().split(",");
        //获取学生解答题所有答案
        String[] stranswer_questions = completedtests.getAnswer_questions_answer().split("\\|&&\\|");
        //遍历答案
        for (int i = 0; i < stranswer_questions.length; i++) {
            String[] ss = stranswer_questions[i].split(":");
            //遍历题目与答案匹配
            for (int j = 0; j < answer_questions.length; j++) {
                if (answer_questions[j].equals(ss[0])) {
                    //封装数据
                    Allquestions allquestions = new Allquestions();
                    allquestions.setQuestion(
                            studentMapper.findAnswer_Questions(Integer.parseInt(ss[0])).getTitle());
                    allquestions.setTitleid(Integer.parseInt(ss[0]));
                    allquestions.setTitleType(3);
                    if (ss.length == 1) {
                        allquestions.setAnswer("");
                    } else
                        allquestions.setAnswer(ss[1]);
                    //存到集合
                    list.add(allquestions);
                }
            }
        }


        //获取作文题目ID
        String[] compositions = titleids.getComposition().split(",");
        //获取学生作文题所有答案
        String[] strcompositions = completedtests.getComposition_answer().split("\\|&&\\|");
        //遍历答案
        for (int i = 0; i < strcompositions.length; i++) {
            String[] ss = strcompositions[i].split(":");
            //遍历题目与答案匹配
            for (int j = 0; j < compositions.length; j++) {
                if (compositions[j].equals(ss[0])) {
                    //封装数据
                    Allquestions allquestions = new Allquestions();

                    allquestions.setQuestion(
                            studentMapper.findComposition(Integer.parseInt(ss[0])).getTitle());
                    allquestions.setTitleid(Integer.parseInt(ss[0]));
                    allquestions.setTitleType(4);
                    if (ss.length == 1) {
                        allquestions.setAnswer("");
                    } else
                        allquestions.setAnswer(ss[1]);
                    //存到集合
                    list.add(allquestions);
                }
            }
        }


        return list;
    }


}
